<?php
    $top_menu_1 = FALSE;
    $top_menu_2 = FALSE;
    $top_menu_3 = FALSE;
    $top_menu_4 = FALSE;
    $top_menu_5 = FALSE;
    $top_menu_6 = FALSE;
    $top_menu_7 = FALSE;
    include('header.php');
    include('sidebar2.php');
    
    $m_levelx = 0;
    if(isset($_SESSION['m_level'])){
        if($_SESSION['m_level']==1){
            $m_levelx = 1;
        }else{
            $m_levelx = 3;
        }
    }
    
    if(isset($_GET['id'])){
        $sql_relate = 'SELECT * FROM hs_product WHERE user_allow<='.$m_levelx.' AND p_active=1 AND c_id='.$_GET['id'].' ORDER BY p_id DESC';
        $pagecat = '&id='.$_GET['id'];
        $catName = $db->queryUniqueValue('SELECT c_name FROM hs_category WHERE c_id='.$_GET['id']);
        $product_page_title = '<div id="product_cat_name"><h2><a href="product.php?id='.$_GET['id'].'" title="'.$catName.'">'.$catName.'</a></h2></div>';
    }else{
        $sql_relate = 'SELECT * FROM hs_product WHERE user_allow<='.$m_levelx.' AND p_active=1 ORDER BY p_id DESC';
        $pagecat = '';
        $product_page_title = '';
    }
    
    // PAGE LIMIT
    if(isset($_GET['p'])){
	$page = $_GET['p'];
    }else{
	$page = 1;
    }
    
    $page_limit = 16;
    $grouplimit = 10;
    $start_row = (($page-1))*$page_limit;
    $sql_relate_nolimit = $sql_relate;
    $sql_relate .= " LIMIT $start_row,$page_limit";
    // PAGE LIMIT END

    // PAGE LIMIT
    $count_row = mysql_query($sql_relate_nolimit);
    $count_row2 = mysql_num_rows($count_row);
    $count_row3 = ceil($count_row2/$page_limit) ;
    
    if($count_row3>1)
    {
	if($count_row3<$grouplimit)
	{
	    for($i = 1 ; $i<=$count_row3 ; $i++) {
		if($i==$page){
		    $limitlink .=  "<span>".$i."</span>&nbsp;";
		}else{
		    $limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
		}
	    }
	}
	else
	{
	    $pagegroup = ceil($page/$grouplimit); // Find group of current page
	    $allpagegroup = ceil($count_row3/$grouplimit); // Total page group
	    if($pagegroup==1) // If First Group
	    {
		if($page>1)
		{
		    $limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;";
		}
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=($pagegroup*$grouplimit);
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .= "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		$limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
	    }
	    else if($pagegroup==$allpagegroup) // If Last Group
	    {
		$limitlink = "<a href='?".$catselect."p=1".$pagecat."' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=$count_row3;
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .= "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		if($page<$count_row3)
		{
		    $limitlink .= "&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		    $limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
		}
	    }
	    else
	    {
		$limitlink = "<a href='?".$catselect."p=1".$pagecat."' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
		$i=($pagegroup*$grouplimit)-($grouplimit-1);
		$j=($pagegroup*$grouplimit);
		for($i ; $i<=$j ; $i++) {
		    if($i==$page){
			$limitlink .=  "<span>".$i."</span>&nbsp;";
		    }else{
			$limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
		    }
		}
		$limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
		$limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
	    }
	}
    }
    
    $rss = $db->query($sql_relate);
    $i = 0;
    $product_list = '';
    while ($line = $db->fetchNextObject($rss)){
        $class='';
        if((($i++)%4)==0){
            $class='product_list_first';
        }
	
	if($line->p_img!='' AND file_exists(APPPATH.'/images/products/product/'.$line->p_id.'/'.$line->p_img)){
            $productImg = URL.'/images/products/product/'.$line->p_id.'/'.$line->p_img;
        }else{
            $productImg = URL.'/images/noimg.jpg';
        }
	
        $product_list .= <<<plist
        <div class="product_list {$class}">
            <a href="function/load_product.php?id={$line->p_id}" title="{$line->p_name}" class="aFancyBox"><img src="{$productImg}" alt="{$line->p_name}" class="img_link" /></a>
        </div>
plist;
    }
?>
            <div id="main_content" class="product_content">
                <img src="images/product_title.jpg" alt="Product" id="product_title" />
                
                <?php echo $product_page_title; ?>
                
                <?php
		    if($m_levelx OR $page==1){
			echo $product_list;
		    }else{
			echo '<div id="register_to_see">Please <a href="register.php" title="Register">register</a> to see more.</div>';
		    }
		?>
                
                <div class="clear">&nbsp;</div>
                
                <div id="product_list_ctrl">
                    <?php
			if($m_levelx OR $page==1){
			    echo $limitlink;
			}
		    ?>
                </div>
            </div>
            <div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>
<script type="text/javascript" src="js/jquery.tinyscrollbar.min.js"></script>
<script>
    $(document).ready(function() {
        $("a.aFancyBox").fancybox({
                'speedIn'		:	600, 
                'speedOut'		:	200, 
                'overlayShow'	:	false,
                'titleShow'	:	false,
                'transitionIn'	:	'elastic',
                'transitionOut'	:	'elastic',
                'opacity' : true,
                'padding' : 0,
                'margin' : 0,
                'showCloseButton' : false,
		onComplete	:	function() {
		    $('#product_detail__').tinyscrollbar();
		}
        });
            
    });
    
    function fancybox_close(){
        $.fancybox.close();
    }
</script>